enyo.kind({
	name: "enyo.CreateAssessmentType",
	kind: enyo.PanelBase,
	
	components: [
		//{kind: "Button", onclick: "btnTestClick", style: "margin-right: 10px", caption: "TEST"},
		{kind: "PanelContent", headerContent: "Select Type", onSignOut: "doSignOut", submitLeftContent: "Next", onSubmitLeft: "submitLeft", onBack: "back", flex: 1, components: [
			{className: "margin-medium", components: [
				{kind: "Group", caption: "Assessment Type", components:[
					{kind: "Picker", name: "typePicker", caption: "Choose Type", value: "-1", items: [], onChange: "typePickerChanged", className: "bold"},
				]},
				{kind: "RowGroup", name: "languageGroup", defaultKind: "HFlexBox", caption: "Supported Language", components:[
					{kind: "Repeater", name: "languageRepeater", onSetupRow: "languageRepeaterSetupRow"}
				]},
				{kind: "Group", name: "descriptionGroup", caption: "", components:[
					{kind: "HtmlContent", name: "descriptionContent", content: "", className: "assessment-type-desc"}
				]}
			]}
		]},
		{name: "typeListService", kind: "WebService", method: "POST", onSuccess: "typeListServiceGot", onFailure: "typeListServiceGotFailure"}
	],
	
	//btnTestClick: function() {
		
	//},
	
	create: function() {
		this.inherited(arguments);
		this.initPanel();
	},
	
	submitLeft: function() {
		if(enyo.Data.selected_AnalysisTypePayload == null) {
			//validation
			this.doError("No assessment type is selected");
			return;
		}
	
		this.parentWnd.switchView("create_device");
	},
	
	back: function() {
		this.parentWnd.switchView("home");
	},
	
	initPanel: function()
	{
		this.showScrim();
		
		//Restore to the default status
		enyo.Data.analysisTypePayload = null;
		enyo.Data.selected_AnalysisTypePayload = null;
		this.$.typePicker.setItems([]);
		this.$.typePicker.setCaption("Choose Type");
		this.$.descriptionContent.setContent("Please choose the assessment type");
		
		//hide the groups
		this.$.languageGroup.setShowing(false);
			
		//call the service
		this.$.typeListService.setUrl(enyo.Config.getServiceUrl_RetrieveAnalysisTypes());
		this.$.typeListService.setHeaders(enyo.Config.getCommonServiceHeader());
		this.$.typeListService.call();
	},
	
	typePickerChanged: function(inSender, inIndex) {
		var selectedTypeValue = this.$.typePicker.getValue();
		enyo.Data.selected_AnalysisTypePayload = this.getPayloadByCode(selectedTypeValue);
		
		//show languages
		this.$.languageGroup.setShowing(true);
		
		//show supported local codes for selected payload
		this.$.languageRepeater.render();
		
		//show current assessment information
		this.$.descriptionGroup.setCaption(enyo.Data.selected_AnalysisTypePayload.defaultAnalysisTypeName);
		this.$.descriptionContent.setContent(enyo.Data.selected_AnalysisTypePayload.defaultAnalysisTypeDescription);
	},
	
	checkboxClicked: function(inSender) {
		var index = inSender.name.substring(6);
		
		enyo.Data.selected_AnalysisTypePayload.supportedLocales[index].checked = inSender.getChecked();
	},
	
	languageRepeaterSetupRow: function(inSender, inIndex) {
		if(enyo.Data.selected_AnalysisTypePayload == null) return;	//type not selected, maybe not loaded
		
		if(inIndex < enyo.Data.selected_AnalysisTypePayload.supportedLocales.length ) {
			return {kind: "Item", layoutKind: "HFlexLayout", onclick: "itemClick", components: [
				{content: enyo.Data.selected_AnalysisTypePayload.supportedLocales[inIndex].displayedNameInEnglish, flex: 1},
				{kind: "CheckBox", name: "local_" + inIndex, checked: enyo.Data.selected_AnalysisTypePayload.supportedLocales[inIndex].checked, onChange: "checkboxClicked"}
			]};
		}
	},
	
	typeListServiceGot: function(inSender, inResponse, inRequest) {
		enyo.Data.analysisTypePayload = inResponse;
		if(inResponse.statusCode === 200) {

			//this.$.typePicker.items.length = 0;	//clear the old items
			var assessmentTypes = [];
			for(var i = 0; i < inResponse.payload.length; i++) {
				var payload = inResponse.payload[i];
				assessmentTypes.push({caption: payload.defaultAnalysisTypeName, value: payload.analysisTypeCode});
			}
			this.$.typePicker.setItems(assessmentTypes);
			this.hideScrim();	//finally, initialize work done, chagne the UI
		}
		else {
			this.doError("Error response of service (StatusCode: " + inResponse.statusCode + ")");
		}
	},
	
	typeListServiceGotFailure: function(inSender, inResponse, inRequest) {
		this.doError("Failed calling service");
	},
	
	getPayloadByCode: function(analysisTypeCode) {
		for(var i = 0; i < enyo.Data.analysisTypePayload.payload.length; i++) {
			if(enyo.Data.analysisTypePayload.payload[i].analysisTypeCode === analysisTypeCode) {
				return enyo.Data.analysisTypePayload.payload[i];
			}
		}
		return null;
	}
});
